home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 501-525 / disk_504 / viewdir / viewdir.asm < prev    next >
Assembly Source File  |  1992-05-06  |  16KB  |  540 lines

  1. ;   ViewDir 2.1/ size directory - Jim Butterfield.  May 12/910.
  2. ; Exec library calls
  3. _LVOAllocMem          EQU -$C6
  4. _LVOFreeMem           EQU -$D2
  5. _LVOSetSignal         EQU -$132
  6. _LVOCloseLibrary      EQU -$19E
  7. _LVOOpenLibrary       EQU -$228
  8. ; DOS library calls
  9. _LVOOpen              EQU -$1E
  10. _LVOClose             EQU -$24
  11. _LVORead              EQU -$2A
  12. _LVOWrite             EQU -$30
  13. _LVOOutput            EQU -$3C
  14. _LVOLock              EQU -$54
  15. _LVOUnLock            EQU -$5A
  16. _LVOExamine           EQU -$66
  17. _LVOExNext            EQU -$6C
  18. _LVOCurrentDir        EQU -$7E
  19. _LVOIoErr             EQU -$84
  20. ;
  21. ACCESS_READ            equ -2
  22. MODE_OLDFILE           equ 1005
  23. INFOSIZE               equ 260
  24. DATASIZE               equ 100
  25. ; FileInfoBlock items
  26. fib_FileName           equ 8
  27. fib_EntryType          equ 120
  28. fib_Size               equ 124
  29. fib_NumBlocks          equ 128
  30. ; IO Error Type
  31. NO_MORE_ENTRIES        equ 232
  32. ;
  33. ;-- Initial setup:
  34. ;        offsets from A5
  35. tSize    equ  -4
  36. tCount   equ  -8
  37. dosBase  equ  -12
  38. argv     equ  -16
  39. OutHandle equ -20
  40. DataBlk  equ  -24
  41. Blocker  equ  -25    ;byte
  42. KillInf  equ  -26    ;byte
  43. Look     equ  -27    ;byte
  44. FileErr  equ  -28    ;byte
  45. Level    equ  -30    ;word
  46. String   equ  -42    ;12 bytes
  47.  
  48.        link   a5,#-44         ; common work area
  49.        moveq  #0,d1
  50.        move.l d1,FileErr(a5)  ; four bytes
  51. trsp   move.b d1,-1(a0,d0)    ; c style string
  52.        move.b -2(a0,d0),d1
  53.        cmp.b  #$20,d1
  54.        bne.s  lastCh
  55.        subq.w #1,d0
  56.        bra.s  trsp
  57. lastCh sub.b  #$22,d1
  58.        bne.s  spdump
  59. ; kill trailing quote
  60.        move.b d1,-2(a0,d0)
  61. spdump move.b (a0)+,d0
  62.        cmp.b  #$20,d0
  63.        beq.s  spdump
  64.        cmp.b  #$22,d0         ; leading quote
  65.        beq.s  skipq
  66.        cmp.b  #$2d,d0         ; dash
  67.        beq.s  fgdump
  68.        sub.b  #'?',d0
  69.        beq.s  hook
  70.        bra.s  spbak
  71. fgdump move.b (a0)+,d0
  72.        beq.s  hook
  73.        and.b  #$5f,d0
  74.        cmp.b  #$49,d0         ; -i
  75.        bne.s  NotI
  76.        move.b d0,KillInf(a5)
  77.        bra.s  fgdump
  78. NotI   cmp.b  #$54,d0
  79.        bne.s  NotT
  80.        move.b d0,Look(a5)
  81.        bra.s  fgdump
  82. NotT   cmp.b  #$42,d0         ; -b
  83.        bne.s  spdump
  84.        move.b d0,Blocker(a5)
  85.        bra.s  fgdump
  86. ; skip leading quote
  87. skipq  addq.l #1,a0
  88.        bra.s  spq
  89. spbak  move.b -(a0),d0
  90. spq    move.l a0,argv(a5)
  91. ;**  Level=0;  tCount=0;
  92. hook   moveq  #0,d1
  93.        move.l d1,Level(a5)
  94.        move.l d1,tCount(a5)
  95. ; set up dos, output, here
  96.        movem.l a0/a1/d0/d1,-(A7)
  97.        move.b #1,FileErr(a5)
  98.        lea    dosname(pc),a1    ; Name 'dos.library'.
  99.        clr.l  d0                ;   Any version (0)
  100.        move.l $4,a6             ;   Using Exec library
  101.        jsr    _LVOOpenLibrary(a6)  ;   Open Dos library.
  102.        move.l d0,dosBase(a5)    ; Remember DosBase ptr.
  103.        beq    StartupQuit       ;      dos not opened
  104.        move.l d0,a6             ; set Dos library
  105.        jsr    _LVOOutput(a6)    ;   get CLI outhandle,
  106.        move.l d0,OutHandle(a5)       ;   & then remember it.
  107.        movem.l (a7)+,a0/a1/d0/d1
  108.        tst.l  d0                ; no file name?
  109.        beq.s  Prompt
  110. ;**    GetDir(argv[1]);
  111.        move.l $4,a6             ; this calls for Exec
  112.        moveq  #0,d1             ; any type of memory
  113.        moveq  #DATASIZE,d0      ; size of block needed
  114.        jsr    _LVOAllocMem(a6)  ; go get it
  115.        move.l dosBase(a5),a6    ; restore Dos Library
  116.        move.l d0,DataBlk(a5)    ; save the block address
  117.        beq.s  DosQuit           ; quit if no memory
  118.        move.l argv(a5),a0       ; file name
  119.        bsr.s  GetDir
  120.        move.l $4,a6             ; set Exec library
  121.        move.l DataBlk(a5),a1    ; memory block address
  122.        moveq  #DATASIZE,d0      ; size of block
  123.        jsr    _LVOFreeMem(a6)   ; release this memory
  124. DosQuit  move.l dosBase(a5),a6  ; set Dos Library
  125. ;**    switch (FileErr)
  126.        subq.b #1,FileErr(a5)
  127.        bne.s  Ncase1
  128. ;**      case 1:
  129. ;**        printf("Out of Memory\n");
  130.        move.l OutHandle(a5),d1       ;file handle
  131.        lea    NoMemMsg(pc),a1
  132.        move.l a1,d2             ;outbuffer
  133.        moveq  #NoMemLen,d3      ;length
  134.        jsr    _LVOWrite(A6)
  135.  
  136. ;**      case 2:
  137. ;**        printf("Can't find file.\n");
  138. Ncase1 subq.b #1,FileErr(a5)
  139.        bne.s  Ncase2
  140.        move.l OutHandle(a5),d1       ;file handle
  141.        lea    NoFileMsg(pc),a1
  142.        move.l a1,d2             ;outbuffer
  143.        moveq  #NoFileLen,d3     ;length
  144.        jsr    _LVOWrite(A6)
  145.  
  146. ;**      case 3:
  147. ;**        printf("^C\n");
  148. Ncase2 subq.b #1,FileErr(a5)
  149.        bne.s  endSwitch
  150.        move.l OutHandle(a5),d1       ;file handle
  151.        lea    CtrlCMsg(pc),a1
  152.        move.l a1,d2
  153.        moveq  #CtrlCLen,d3
  154.        jsr    _LVOWrite(A6)
  155. endSwitch   bra.s  LastGasp
  156. ;**      }
  157. ;**    }
  158. ;**  else printf("FORMAT:  ViewDir <DirectoryName>\n");
  159. Prompt      move.l OutHandle(a5),d1
  160.        lea    PromptMsg(pc),a1
  161.        move.l a1,d2
  162.        moveq  #PromptLen,d3
  163.        jsr    _LVOWrite(A6)
  164. ;**  }
  165. LastGasp:   move.l dosBase(a5),a1    ; DosBase to a1;
  166.        move.l $4,a6             ;   then using Exec library,
  167.        jsr    _LVOCloseLibrary(a6)    ;   close Dos library.
  168. StartupQuit unlk   a5
  169.        rts                      ; End of Program
  170.  
  171. ;**void GetDir(char * Name)
  172. ;**  {
  173. ;**  unsigned long Size, Count, newLock, oldLock;
  174. ;**  struct FileInfoBlock *fibb;
  175. ;**  char *NameSave;
  176. ;            offsets from A4
  177. Size        equ    -4
  178. Count       equ    -8
  179. newLock     equ    -12
  180. oldLock     equ    -16
  181. fibb        equ    -20
  182. NameSave    equ    -24    ;drawer (calling) name
  183. xName       equ    -28    ;name to be printed
  184. fType       equ    -32    ;dir or file
  185.  
  186. GetDir link   A4,#-32
  187.        move.l a0,xName(a4)
  188.        move.l a0,NameSave(a4)
  189. ;**  Size = 0;
  190. ;**  Count =0;
  191. ;**  Count = 0;
  192.        moveq  #0,d0
  193.        move.l d0,Size(a4)
  194.        move.l d0,Count(a4)
  195. ;**  FileErr =1;   /* signal Alloc error */
  196.        move.b #1,FileErr(a5)
  197. ;**  if ((fibb = AllocMem(INFOSIZE,0)) != 0)
  198.        move.l $4,a6             ; this calls for Exec
  199.        moveq  #0,d1             ; any type of memory
  200.        move.l #INFOSIZE,d0      ; size of block needed
  201.        jsr    _LVOAllocMem(a6)  ; go get it
  202.        move.l dosBase(a5),a6    ; restore Dos Library
  203.        move.l d0,fibb(a4)       ; save the block address
  204.        beq    GDExit            ; quit if no memory
  205. ;**    {
  206. ;**      FileErr =2;   /* signal file unreadable */
  207.        move.b #2,FileErr(a5)
  208. ;**      if ((newLock = Lock(Name,ACCESS_READ)) != 0)
  209.        move.l xName(a4),d1
  210.        moveq  #ACCESS_READ,D2   ; get a read lock
  211.        jsr    _LVOLock(a6)
  212.        move.l d0,newLock(a4)    ; store the lock pointer
  213.        beq    DropMem           ; couldn't get lock
  214. ;**      { if ((Examine(newLock,fibb)) != 0)
  215.        move.l d0,d1             ; lock
  216.        move.l fibb(a4),d2       ; file info block address
  217.        jsr _LVOExamine(a6)      ; go for the data
  218. ;   Assume Examine never fails.  Go for the data.
  219. ;**        {
  220. ;**        FileErr =0;    /* got file, normal error sequence */
  221.        move.b #0,FileErr(a5)
  222. ;**         {
  223. ;**              if (fibb->fib_EntryType < 0)
  224.        move.l fibb(a4),a3
  225.        move.l fib_EntryType(a3),d0
  226.        move.l d0,fType(a4)
  227.        bpl.s  TypeDir2
  228. ;**                {
  229. ;**                Size = fibb->fib_Size;
  230. ;**                Blox = fibb->fib_NumBlocks;
  231. ;**                Count = 1;
  232. ;**                }  /* file */
  233.        move.l NameSave(a4),a0
  234.        jsr    FilBit
  235.        bra    NextItem
  236. ;**              else
  237. TypeDir2:
  238. ;**           {
  239. ;**                oldLock = CurrentDir(newLock);
  240.        move.l newLock(a4),d1
  241.        jsr    _LVOCurrentDir(a6)
  242.        move.l d0,oldLock(a4)
  243. ;**                Level += 1;
  244.        addq.w #1,Level(a5)
  245. DirLoop:
  246. ;**    {
  247. ;**            if ((ExNext(newLock,fibb)) == 0)
  248.        move.l newLock(a4),d1   ; lock
  249.        move.l fibb(a4),d2      ; file info block address
  250.        jsr    _LVOExNext(a6)      ; go for the data
  251.        tst.l  d0
  252.        beq.s  EndDir
  253.        tst.b  FileErr(a5)
  254.        bne.s  EndDir
  255. ;**              if (fibb->fib_EntryType < 0)
  256.        move.l fib_EntryType(a3),d0
  257.        bpl.s  TypeDir3
  258.        lea    fib_FileName(a3),a0
  259.        jsr    FilBit
  260.        bra.s  Join3
  261. ;**                GetDir(fibb->fib_FileName);
  262. ;**                Size += tSize;
  263. ;**                Count += tCount;
  264. TypeDir3  lea    fib_FileName(a3),a0
  265.        bsr    GetDir
  266. Join3  move.l tSize(a5),d0
  267.        add.l  d0,Size(a4)
  268.        move.l tCount(a5),d0
  269.        add.l  d0,Count(a4)
  270. ; collect stats?
  271.        move.l fibb(a4),a3
  272.        bra.s  DirLoop
  273. ;**            Level -= 1;
  274. EndDir subq.w #1,Level(a5)
  275. ;**                newLock = CurrentDir(oldLock);
  276.        move.l oldLock(a4),d1
  277.        jsr    _LVOCurrentDir(a6)
  278.        move.l d0,newLock(a4)
  279. ;**                }  /* dir */
  280. Rejoin2:
  281. ;**              ??Count += Count;
  282.        tst.b  FileErr(a5)
  283.        bne.s  EndSplit1
  284.        move.l Size(a4),d7
  285. ;**              if (Level <1 0)
  286.        move.w Level(a5),d0
  287.        beq.s  Dir3     ; assuming a directory
  288.        subq.w #1,d0
  289.        bne.s  NextItem
  290. ;**                {
  291. ;**                else
  292. ;**                      printf(" DIR %3d",Count);
  293. Dir3   move.l OutHandle(a5),d1
  294.        lea    DirMsg(pc),a1
  295.        move.l a1,d2
  296.        moveq  #DirLen,d3
  297.        jsr    _LVOWrite(A6)
  298.        move.l Count(a4),d0
  299.        bsr    MakeAscii
  300.        move.l OutHandle(a5),d1
  301.        lea    String+5(A5),a1
  302.        move.l a1,d2
  303.        moveq  #5,d3
  304.        jsr    _LVOWrite(A6)
  305. ;**                printf(" %7d %4d %s\n",Size,Blox,NameSave);
  306.        move.l NameSave(a4),a2
  307.        bsr    PrintStuff
  308. ;**                }
  309. ; look for file join here
  310. NextItem:
  311.        move.l a6,d7
  312.        moveq  #0,d0
  313.        move.l #$1000,d1
  314.        move.l $4,a6
  315.        jsr    _LVOSetSignal(a6)
  316.        move.l d7,a6
  317.        and.l  #$1000,d0
  318.        beq.s  EndSplit1
  319.        move.b #3,FileErr(a5)
  320. EndSplit1:
  321. ;**   tSize = Size;
  322. ;**   tCount = Count;
  323.        move.l Size(a4),tSize(a5)
  324.        move.l Count(a4),tCount(a5)
  325. ;**        }   /* Examine OK */
  326. ;**      UnLock(newLock);
  327. DropLock    move.l dosBase(a5),a6    ; set Dos Library
  328.        move.l newLock(a4),d1    ; take off the lock
  329.        jsr    _LVOUnLock(a6)
  330. ;**      }      /* Lock OK */
  331. ;**    FreeMem(fibb,INFOSIZE);
  332. DropMem:    move.l $4,a6             ; set Exec library
  333.        move.l fibb(a4),a1       ; memory block address
  334.        move.l #INFOSIZE,d0      ; size of block
  335.        jsr    _LVOFreeMem(a6)   ; release this memory
  336.        move.l dosBase(a5),a6    ; set Dos Library
  337. ;**    }        /* Alloc OK */
  338. GDExit unlk   A4
  339. FilEnd rts
  340. ;**  }
  341.  
  342. FilBit move.l  a0,xName(a4)
  343.        move.l  fib_Size(a3),d7
  344.        tst.b   Blocker(a5)
  345.        beq.s   noblox2
  346.        move.l  fib_NumBlocks(a3),d7
  347. noblox2 move.l d7,tSize(a5)
  348.        moveq   #1,d0
  349.        move.l  d0,tCount(a5)
  350. ;**              if (Level <1 0)
  351.        move.w Level(a5),d0
  352.        beq.s  pf          ; assuming a file
  353.        subq.w #1,d0
  354.        bne.s  FilEnd
  355. ; Check .info status
  356. pf     tst.b  KillInf(a5)
  357.        beq.s  PeekFile
  358.        move.l xName(a4),a2
  359. TryCharx  move.b (a2)+,d0
  360.        bne.s  TryCharx
  361.        lea    -6(a2),a2
  362.        lea    InfoLine(pc),a1
  363. InfLp  cmp.b  (a1)+,(a2)+
  364.        beq.s  InfLp
  365.        tst.b  -1(a2)
  366.        beq.s  FilEnd
  367. ; We're gonna print the file.  Empty?  Will we look at it?
  368. PeekFile lea   EmptyMsg(pc),a2
  369.        tst.l  d7
  370.        beq    FlEmp
  371.        tst.b  Look(a5)
  372.        bne    DoNotLook
  373. ; Open the file in order to check details
  374.        move.l xName(a4),d1   ; filename pointer
  375.        move.l #MODE_OLDFILE,d2  ; get a read lock
  376.        jsr    _LVOOpen(a6)
  377.        move.l d0,d6             ; store the file handle
  378.        beq    FlEmp             ; couldn't get file
  379. ; Read DATASIZE bytes of the file, then close it
  380.        move.l d6,d1             ;infile handle
  381.        move.l DataBlk(a5),d2    ; input buffer address
  382.        moveq  #DATASIZE,d3      ; how many bytes
  383.        jsr    _LVORead(a6)      ; so read it
  384.        move.l d0,d5             ; actual bytes read
  385.        move.l d6,d1             ; inhandle
  386.        jsr    _LVOClose(a6)     ; close the file
  387.        lea    TextMsg(pc),a2
  388. ; check for text characters only
  389.        move.l DataBlk(a5),a0
  390.        moveq  #0,d3
  391. TxLp   move.b 0(a0,d3),d0
  392.        cmp.b  #9,d0
  393.        beq.s  TryMore
  394.        cmp.b  #10,d0
  395.        beq.s  TryMore
  396.        cmp.b  #$20,d0
  397.        blt.s  NoText
  398. TryMore addq.w #1,d3
  399.        cmp.w  d3,d5
  400.        bne.s  TxLp
  401.        bra    FlEmp 
  402. ; Not Text - try longword start
  403. NoText move.l (a0),d0
  404.        lea    LdblMsg(pc),a2
  405.        cmp.l  #$3F3,d0
  406.        beq.s  FlEmp
  407. ; special IFF sequence
  408.        cmp.l  #'FORM',d0
  409.        bne.s  WordStart       
  410.        lea    String(a5),a2
  411.        move.l #'IFF:',(a2)
  412.        move.l 8(a0),4(a2)
  413.        bra.s  FlEmp
  414. ; look for magic word start
  415. WordStart  move.w (a0),d0
  416.        lea    WBOMsg(pc),a2
  417.        cmp.w  #$E310,d0
  418.        beq.s  FlEmp
  419.        lea    FLisMsg(pc),a2
  420.        cmp.w  #$f00,d0
  421.        beq.s  FlEmp
  422.        lea    WBWMsg(pc),a2
  423.        cmp.w  #$f34c,d0
  424.        beq.s  FlEmp
  425. ; special Basic sequence
  426.        and.w  #$FE7F,d0
  427.        lea    BasMsg(pc),a2
  428.        cmp.w  #$F400,d0
  429.        beq.s  FlEmp
  430. ; special longword-text test
  431.        moveq  #3,d1
  432. lngtst move.b 0(a0,d1.w),d0
  433.        cmp.b  #$30,d0
  434.        blt.s  DoNotLook
  435.        cmp.b  #$7f,d0
  436.        bgt.s  DoNotLook
  437.        dbf    d1,lngtst
  438.        lea    String(a5),a2
  439.        move.l #'....',(a2)
  440.        move.l (a0),4(a2)
  441.        bra.s  FlEmp
  442.  
  443.  
  444. DoNotLook lea FileMsg(pc),a2
  445. FlEmp  move.l a2,d2
  446.        move.l OutHandle(a5),d1
  447.        moveq  #FileLen,d3
  448.        jsr    _LVOWrite(a6)
  449.        move.l xName(a4),a2
  450. ; PeekFile drops into PrintStuff
  451. ;     ...or PS may be called directly...
  452. PrintStuff:
  453.        move.l d7,d0
  454.        bsr.s  MakeAscii
  455.        move.l OutHandle(a5),d1
  456.        lea    String(a5),a1
  457.        moveq  #11,d3
  458.        tst.b  Blocker(a5)
  459.        beq.s  noblox3
  460.        lea    String+4(a5),a1
  461.        moveq  #7,d3
  462. noblox3   move.l a1,d2
  463.        jsr    _LVOWrite(a6)
  464.        move.w Level(a5),d0
  465.        bne.s  Flush
  466.        move.l OutHandle(a5),d1
  467.        lea    SpaceMsg(pc),a1
  468.        move.l a1,d2
  469.        moveq  #SpaceLen,d3
  470.        jsr    _LVOWrite(A6)
  471. Flush  move.l OutHandle(a5),d1
  472.        move.l a2,d2
  473.        moveq  #0,d3
  474. TryChara  move.b 0(a2,d3),d0
  475.        beq.s  FoundEnd
  476.        addq.b #1,d3
  477.        bra.s  TryChara
  478. FoundEnd  jsr    _LVOWrite(A6)
  479.        move.l OutHandle(a5),d1
  480.        lea    MsgNewLine(pc),a1
  481.        move.l a1,d2
  482.        moveq  #NewLineLen,d3
  483.        jsr    _LVOWrite(A6)
  484. PrEnd  rts
  485.  
  486. MakeAscii:
  487.        moveq  #$20,d1
  488.        lea    String(a5),a1
  489.        moveq  #11,d2
  490. SpacedOut   move.b d1,0(a1,d2)
  491.        dbf    d2,SpacedOut
  492.        moveq  #9,d2
  493. DigLoop     swap   d0
  494.        moveq  #0,d1
  495.        move.w d0,d1
  496.        beq.s  Skipper
  497.        divu   #10,d1
  498. Skipper     move.w d1,d0
  499.        swap   d0
  500.        move.w d0,d1
  501.        divu   #10,d1
  502.        move.w d1,d0
  503.        swap   d1
  504.        or.b   #$30,d1
  505.        move.b d1,0(a1,d2)
  506.        tst.l  d0
  507.        dbeq   d2,DigLoop
  508.        rts
  509.  
  510. InfoLine    dc.b   '.info.'
  511. MsgNewLine  dc.b   $0a
  512. NewLineLen  equ    *-MsgNewLine
  513. SpaceMsg    dc.b   $20
  514. SpaceLen    equ    *-SpaceMsg
  515. DirMsg      dc.b   'DIR'
  516. DirLen      equ    *-DirMsg
  517.             dc.b   '$VER:'
  518. PromptMsg   dc.b   'ViewDir (by Jim Butterfield) V2.1',$0a
  519.             dc.b   'FORMAT:  ViewDir [-b -i -t] directory',$0a
  520. PromptLen   equ    *-PromptMsg
  521. TextMsg     dc.b   'TextFile'
  522. LdblMsg     dc.b   'Loadable'
  523. WBOMsg      dc.b   'WBObject'
  524. WBWMsg      dc.b   'WBWindow'
  525. BasMsg      dc.b   'AmiBasic'
  526. FLisMsg     dc.b   'FontList'
  527. FileMsg     dc.b   'FILE    '
  528. FileLen     equ    *-FileMsg
  529. EmptyMsg    dc.b   'Empty   ',$0a
  530. EmptyLen    equ    *-EmptyMsg
  531. NoMemMsg    dc.b   'Out of Memory',$0a
  532. NoMemLen    equ    *-NoMemMsg
  533. NoFileMsg   dc.b   'Can t find file.',$0a
  534. NoFileLen   equ    *-NoFileMsg
  535. CtrlCMsg    dc.b   '^C',$0a
  536. CtrlCLen     equ    *-CtrlCMsg
  537. dosname     dc.b   'dos.library',0
  538.             end
  539.  
  540.